<?php


namespace app\hapi\controller;


use app\common\model\AuthGroup;
use app\common\model\AuthRule;
use app\common\model\Sysuser;
use think\Cache;
use think\Controller;
use think\Cookie;

class Login extends Controller
{
    public function getlogin($param)
    {
        $Sysuser = new Sysuser();
        $map['username'] = $param['username'];
        $info = $Sysuser->where($map)->find();
        if ($info) {
            if ($info['password'] == md5($param['password'] . $info['salt'])) {
                //清空其他人员登陆信息
                Cache::store('redis')->clear($info['id']);
                $user_param['uid'] = $info['id'];
                $user_param['name'] = $info['name'];
                $user_param['username'] = $info['username'];
                $user_param['group'] = $info['group'];
                $user_param['agentid']=$info['agentid'];
                $user_param['parkid']=$info['parkid'];
                $token = generateNonceStr(42);
                Cache::store('redis')->tag($info['id'])->set($token, json_encode($user_param));
                $re_data['status'] = 1;
                $re_data['data'] = [
                    'token' => $token,
                ];
                $re_data['return_code'] = "SUCCESS";
                $re_data['return_msg'] = "成功";
            } else {
                $re_data['return_code'] = "FAIL";
                $re_data['return_msg'] = "账号密码错误";
            }
        } else {
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = "账号密码错误";
        }
//         返回JSON数据格式到客户端 包含状态信息
        return $re_data;

    }

    public function getuserinfo($param)
    {
        $cacheinfo = Cache::store('redis')->get($param['token']);

        if ($cacheinfo) {
            $cacheinfo = json_decode($cacheinfo,true);
            $map['id'] = $cacheinfo['uid'];
            $Sysuser = new Sysuser();
            $info = $Sysuser->where($map)->field('group,name,avatar,username')->find();
            if ($info) {
                $result['roles'] = self::getrole($info['group']);
                $result['name'] = $info['name'];
                $result['avatar'] = $info['avatar'];
                $result['introduction'] = "editor";
                $re_data['data'] = $result;
                $re_data['return_code'] = "SUCCESS";
                $re_data['return_msg'] = "成功";
            }
        } else {
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = "登录状态异常";
        }
        // 返回JSON数据格式到客户端 包含状态信息
        return $re_data;

    }

    private function getrole($group)
    {
        $map['id'] = $group;
        $map['status'] = 1;
        $AuthGroup = new AuthGroup();
        $rules = $AuthGroup->where($map)->value('rules');
        $mab['id'] = array('in', $rules);
        $AuthRule = new AuthRule();
        $menu = $AuthRule->where($mab)->column('name');
        return $menu;
    }

    public function logout($param)
    {
        cookie($param['token'], null);
        $re_data['return_code'] = "SUCCESS";
        $re_data['return_msg'] = "成功";
        return $re_data;

    }

}